package com.dhcc.core.modules.system.service.impl;

import java.io.File;
import java.util.List;
import java.util.UUID;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.dhcc.core.config.properties.SysProperties;
import com.dhcc.core.framework.base.service.impl.BaseServiceImpl;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.util.DateUtil;
import com.dhcc.core.modules.system.dao.PanAttachmentMapper;
import com.dhcc.core.modules.system.entity.PanAttachment;
import com.dhcc.core.modules.system.service.IPanAttachmentService;

/**
 * 表数据服务层接口
 * 
 * @ClassName: PanAttachmentServiceImpl
 * @Description: TODO
 * @author mk
 * @date 2018-08-31 10:07:21
 */
@Service
@Transactional(readOnly = true)
public class PanAttachmentServiceImpl extends BaseServiceImpl<PanAttachmentMapper, PanAttachment> implements IPanAttachmentService {

	/**
	 * 删除文件
	 * @param attachmentId
	 */
	@Override
	@Transactional(rollbackFor = Throwable.class)
	public void deleteAttachmentById(Long attachmentId) {
	    PanAttachment attachment = this.selectById(attachmentId);
		if(attachment.getQuote() == 1) {
			String filepath = SysProperties.me().getFileUploadPath() + attachment.getFilePath();
			File file = new File(filepath);
			file.delete();
			String dilectory = filepath.substring(0, filepath.lastIndexOf("\\"));
			File filedilectory = new File(dilectory);
			File[] listFiles = filedilectory.listFiles();
			if (listFiles.length == 0) {
				filedilectory.delete();
			}
			this.deleteById(attachmentId);
		}else {
		    attachment.setQuote(attachment.getQuote()-1);
		    attachment.updateById();
		}
	}

	/**
	 * 保存文件
	 * @param file
	 * @return
	 */
	@Override
	public PanAttachment saveFile(MultipartFile file) {
		String originalName = file.getOriginalFilename();
		String fileSavePath = "upload" + File.separator +"fileDirectory" + File.separator  + DateUtil.getYearMonth() + File.separator;
		String fileUploadPath = SysProperties.me().getFileUploadPath();
        File dir = new File(fileUploadPath + fileSavePath);
		if (!dir.exists()) {
			dir.mkdirs();
		}
		String fileName = UUID.randomUUID().toString() + originalName.substring(originalName.lastIndexOf("."),originalName.length());
		PanAttachment attachment = new PanAttachment();
		try {
			attachment.setFileSize(file.getSize());
			attachment.setOriginalName(originalName);
			attachment.setFileMimeType(originalName.substring(originalName.lastIndexOf("."),originalName.length()));
			attachment.setFilePath(fileSavePath + fileName);
			attachment.setSystemName(fileName);
			attachment.setQuote(1);
			attachment.insert();
			file.transferTo(new File(fileUploadPath + fileSavePath + fileName));
		} catch (Exception e) {
			e.printStackTrace();
			throw new BizException(BizExceptionEnum.UPLOAD_ERROR);
		}
		return attachment;
	}

	/**
	 * 判断该用户拥有以下哪些文件
	 * @param userId
	 * @param attachmentId
	 * @return
	 */
	@Override
	public List<PanAttachment> hasAttachmentByUserId(Long userId, List<Long> attachmentId) {
		return this.baseMapper.hasAttachmentByUserId(userId, attachmentId);
	}

	 /**
	 * 批量更新引用数
	 * @param quote 正负代表引用数加减
	 * @param attachmentId
	 */
	@Override
	public void updataQuoteByListId(int quote, List<Long> attachmentId) {
		this.baseMapper.updataQuoteByListId(quote, attachmentId);
	}

}